package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ApphomeHeadViewNewBinding;
import sz.szsmk.citizencard.databinding.ApphomeModuleFinanceViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.HomeHeadFuncResponse;
import sz.szsmk.citizencard.model.network.bean.response.HomeWeathRes;
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo;
import sz.szsmk.citizencard.model.network.clarkbean.response.AreaInfo;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.ui.activity.ChargeActivity;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2020
 * author: Clark
 * created on:2020年10月7日 16点06分
 * description: App财富模块
 */
public class AppHomeFinanceAdapter extends DelegateAdapter.Adapter<AppHomeFinanceAdapter.AppHomeFinanceHolder> {
    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private HomeWeathRes weathData;
    private ArrayList<String> mList = new ArrayList<>();
    private ArrayList<Integer> iconList = new ArrayList<>();
    private AreaInfo viewData;
    private ApphomeFinanceGridAdapter apphomeFinanceGridAdapter;
    private BaseInterface baseInterface;


    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, String id, AdInfo bean);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    public AppHomeFinanceAdapter(FragmentActivity activity, DefaultLayoutHelper headHelper) {
        mContext = activity;
        mGridLayoutHelper = headHelper;

    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public AppHomeFinanceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_head_view_new, parent, false);
        return  AppHomeFinanceHolder.creat(parent);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_module_finance_view;
    }

    @Override
    public void onBindViewHolder(AppHomeFinanceHolder holder, int position) {
//        holder.blurringView.setBlurredView();

        loadItemData(position, holder.binding);

    }
    private void loadItemData(int position, ApphomeModuleFinanceViewBinding binding) {
        if (viewData != null) {
            RecyclerView appFinanceGrid = binding.financeList;
            binding.financeTitle.setText(viewData.getResource_area_name());
            binding.financeMore.setText(viewData.getResource_sub_title());
            binding.financeMore.setOnClickListener(v -> {
//                Log.d("clark","clark here");
//                 Bundle bundle=new Bundle();
//                 bundle.putString("url",  viewData.getTitle_url());
//                JumpUtils.jumptoNatvie(mContext, "webView", 1, bundle);
                MessageEvent message = new MessageEvent(MessageEvent.CODE_OPENNG);
                EventBus.getDefault().post(message);
            });
            GridLayoutManager manager = new GridLayoutManager(mContext, 3);
            apphomeFinanceGridAdapter = new ApphomeFinanceGridAdapter(mContext);


            apphomeFinanceGridAdapter.setBaseInterface(new ApphomeFinanceGridAdapter.BaseInterface() {

                @Override
                public void onClick(View view, String url, String syslabl, int need_header, String id, AdInfo bean) {

                    if (baseInterface!=null){
                        baseInterface.onClick(view,url,syslabl,need_header,id,bean);
                    }
                }
            });
            appFinanceGrid.setLayoutManager(manager);
            appFinanceGrid.setAdapter(apphomeFinanceGridAdapter);
            apphomeFinanceGridAdapter.setViewData(viewData);
            apphomeFinanceGridAdapter.notifyDataSetChanged();
        }

    }
    @Override
    public int getItemCount() {
        return 1;
    }


    public void setViewData(AreaInfo viewData) {
        this.viewData = viewData;
    }


    public static class AppHomeFinanceHolder extends RecyclerView.ViewHolder {
        private ApphomeModuleFinanceViewBinding binding;

        public static AppHomeFinanceHolder creat(ViewGroup parent) {
            ApphomeModuleFinanceViewBinding binding =
                    ApphomeModuleFinanceViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);

            return new AppHomeFinanceHolder(binding);
        }

        public AppHomeFinanceHolder(ApphomeModuleFinanceViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

    }

}
